﻿package mortal.game.view.copy.towerCopy
{
    import Message.Public.*;
    import __AS3__.vec.*;
    import com.gengine.resource.*;
    import com.gengine.utils.*;
    import com.mui.controls.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.geom.*;
    import mortal.common.global.*;
    import mortal.component.gconst.*;
    import mortal.component.window.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.manager.window3d.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.tableConfig.*;
    import mortal.game.scene3D.map3D.util.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.copy.towerCopy.cellrender.*;
    import mortal.game.view.copy.towerCopy.other.*;
    import mortal.game.view.copy.towerCopy.panel.*;
    import mortal.game.view.copy.towerCopy.win.*;
    import mortal.game.view.wizard.*;
    import mortal.mvc.core.*;

    public class TowerModule extends BaseWindow
    {
        private var itemList:GTileList;
        private var imgbg2:GImageBitmap;
        protected var _rightTitle:GBitmap;
        protected var _ganTitle:GBitmap;
        protected var _curBossTitle:GBitmap;
        protected var _rdTitle:GBitmap;
        protected var _reCombat:GBitmap;
        protected var numberTxt:BitmapNumberText;
        protected var passTitleTxt:GTextFiled;
        protected var _combatTxt:BitmapNumberText;
        protected var enterCopyBtn:GLoadingButton;
        protected var _sweepBtn:GLoadingButton;
        protected var _getRewardBtn:GLoadingButton;
        protected var _resetBtn:GLoadingButton;
        protected var _sweepGuanDes:GTextFiled;
        protected var _sweepGuanTxt:GTextFiled;
        private var passInfo:TowerPassInfoPanel;
        private var _bar:BaseProgressBar;
        private var _secTimer:SecTimerView;
        private var _tabBar:GTabarNew;
        protected var _rect3d:Rect3DObject;
        public var rect:Rectangle;
        private var _resLoad:Boolean = false;
        private var _totleTime:int;
        private var _scanValue:int = -1;
        public static var copyCode:int;

        public function TowerModule()
        {
            this.rect = new Rectangle(8, 65, 640, 542);
            super(LayerManager.windowLayer3D);
            setSize(895, 605);
            title = "" + Language.getString(71016);
            titleHeight = 57;
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            LoaderHelp.addResCallBack(ResFileConst.tower, this.onResLoad);
            return;
        }// end function

        public function onResLoad() : void
        {
            this._resLoad = true;
            this.init3DRect();
            this._tabBar = UIFactory.gTabBarNew(10, 32, this.tabData, 885, 520, 80, 26, this, this.onTabBarChange, "TabButtonNew");
            this._tabBar.selectedIndex = 0;
            this.imgbg2 = UIFactory.gImageBitmap(ImagesConst.tower_win_right_bg, 655, 65, this);
            this._rightTitle = UIFactory.gBitmap(ImagesConst.tower_win_newDetailTl, 730, 79, this);
            this._ganTitle = UIFactory.gBitmap(ImagesConst.tower_win_newCurGanNumTl, 664, 103, this);
            this._curBossTitle = UIFactory.gBitmap(ImagesConst.tower_win_newGanBossTl, 797, 149, this);
            this._rdTitle = UIFactory.gBitmap(ImagesConst.tower_win_newCurGanRdTl, 778, 224, this);
            this._reCombat = UIFactory.gBitmap(ImagesConst.tower_win_newPWCdTl, 810, 336, this);
            this.numberTxt = UIFactory.bitmapNumberText(847, 105, "GuildWarResultStarNum.png", 26, 27, -13, this, 13, 1);
            this.numberTxt.text = "3";
            this.passTitleTxt = UIFactory.gTextField("", 662, 185, 236, 25, this, GlobalStyle.textFormatPutong.setColor(16777215).setSize(16).center().setBold(true));
            this._bar = UICompomentPool.getUICompoment(BaseProgressBar);
            this._bar.setBg(ImagesConst.MountBarBg, true, 191, 13);
            this._bar.setProgress(ImagesConst.MountBar, false, 4, 4, 183, 13);
            this._bar.setLabel(BaseProgressBar.ProgressBarTextNone, 45);
            this._bar.isOverShowValue = true;
            this._bar.mouseChildren = false;
            this._bar.mouseEnabled = true;
            UIFactory.setObjAttri(this._bar, 680, 575, -1, -1, this);
            this._secTimer = UIFactory.secTimeView("mm:ss", 676, 576, 190, 20, this, GlobalStyle.textFormatBai.center());
            this._secTimer.configEventListener(EventName.SecViewTimeChange, this.onSecViewTimeChange);
            this.itemList = UIFactory.tileList(692, 271, 180, 60, this);
            this.itemList.rowHeight = 42;
            this.itemList.columnWidth = 42;
            this.itemList.horizontalGap = 1;
            this.itemList.verticalGap = 1;
            this.itemList.rowCount = 6;
            this.itemList.setStyle("cellRenderer", TowerRewardsCellRenderer);
            addChild(this.itemList);
            this._combatTxt = UIFactory.bitmapNumberText(773, 376, "SunbathCopyNum1.png", 19, 24, -2, this, 10, 1);
            this._combatTxt.text = "12345";
            this.enterCopyBtn = UIFactory.gLoadingButton(ImagesConst.tower_win_go4_upSkin, 660, 402, 231, 117, this);
            this.enterCopyBtn.label = "";
            this._getRewardBtn = UIFactory.gLoadingButton(ImagesConst.LineOffCopyCellBtn_upSkin, 678, 520, 88, 29, this);
            this._getRewardBtn.label = "" + Language.getString(71017);
            this._sweepBtn = UIFactory.gLoadingButton(ImagesConst.LineOffCopyCellBtn_upSkin, 678, 520, 88, 29, this);
            this._sweepBtn.label = "" + Language.getString(71018);
            this._resetBtn = UIFactory.gLoadingButton(ImagesConst.LineOffCopyCellBtn_upSkin, 786, 520, 88, 29, this);
            this._resetBtn.label = "" + Language.getString(71019);
            this._sweepGuanDes = UIFactory.gTextField("" + Language.getString(71020), 661, 552, 236, 20, this, GlobalStyle.textFormatLv.setSize(13).center().setBold(true));
            this._sweepGuanTxt = UIFactory.gTextField("20", 755, 553, 100, 25, this, GlobalStyle.textFormatHuang2.setSize(13).center().setBold(true));
            this.enterCopyBtn.configEventListener(MouseEvent.CLICK, this.onMouseClick);
            this.enterCopyBtn.name = "TowerCopy_EnterCopyBtn";
            var rankLink:* = "<a href=\'event:positionInroduce\' target = \'\'><font color=\'" + GlobalStyle.colorHuang + "\'><u>" + Language.getString(71021) + "</u></font></a>";
            var onOpenRankPanel:* = function (event:TextEvent) : void
            {
                TowerRankWin.instance.popUp();
                TowerRankWin.instance.x = 805;
                TowerRankWin.instance.y = 5;
                return;
            }// end function
            ;
            this.passInfo = UICompomentPool.getUICompoment(TowerPassInfoPanel, this) as TowerPassInfoPanel;
            this._resetBtn.configEventListener(MouseEvent.CLICK, this.onReset);
            this._sweepBtn.configEventListener(MouseEvent.CLICK, this.onSweep);
            this._getRewardBtn.configEventListener(MouseEvent.CLICK, this.onGetRewardBtn);
            this._getRewardBtn.visible = false;
            NetDispatcher.addCmdListener(ServerCommand.TowerInfoUpdate, this.onTowerInfoUpdate);
            this.onTabBarChange();
            this.onTowerInfoUpdate();
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._scanValue = -1;
            if (this._resLoad == false)
            {
                return;
            }
            this._resLoad = false;
            NetDispatcher.removeCmdListener(ServerCommand.TowerInfoUpdate, this.onTowerInfoUpdate);
            this.imgbg2 = null;
            this._rightTitle = null;
            this._ganTitle = null;
            this._curBossTitle = null;
            this._rdTitle = null;
            this._reCombat = null;
            this.numberTxt = null;
            this.passTitleTxt = null;
            this.itemList = null;
            this._combatTxt = null;
            this.enterCopyBtn = null;
            this._sweepBtn = null;
            this._sweepGuanDes = null;
            this._sweepGuanTxt = null;
            this._tabBar = null;
            this._secTimer = null;
            this._bar = null;
            this._resetBtn = null;
            this._getRewardBtn = null;
            this.passInfo = null;
            this.remove3D();
            return;
        }// end function

        private function get tabData() : Array
        {
            var _loc_1:* = Cache.instance.role.entityInfo.level;
            if (_loc_1 >= 60)
            {
                return Language.getArray(70704);
            }
            return Language.getArray(70703);
        }// end function

        protected function onTabBarChange(event:Event = null) : void
        {
            var _loc_2:* = this._tabBar.dataProvider[this._tabBar.selectedIndex];
            copyCode = _loc_2.name;
            this.updateComBatTxt();
            this.refresh();
            Dispatcher.dispatchEvent(new DataEvent(EventName.TowerGetStatus, copyCode));
            return;
        }// end function

        public function refresh(param1:Boolean = false) : void
        {
            var _loc_4:Vector.<TowerKillInfo> = null;
            var _loc_5:String = null;
            var _loc_6:TowerKillInfo = null;
            var _loc_7:int = 0;
            if (this._resLoad == false)
            {
                return;
            }
            var _loc_2:* = Cache.instance.copy.tower;
            this.itemList.dataProvider = _loc_2.getCurrentPassRewardsDataProvider(4, copyCode);
            this.itemList.drawNow();
            var _loc_3:* = _loc_2.getCurrentLevel(copyCode);
            if (_loc_3 <= GameConst.TConstTowerMaxLevel)
            {
                this.setNumberTxt(_loc_2.getCurrentLevel(copyCode));
                _loc_4 = TowerCopyConfig.instance.getPassConditionByID(copyCode, _loc_2.getCurrentLevel(copyCode));
                _loc_5 = "";
                _loc_7 = 0;
                while (_loc_7 < _loc_4.length)
                {
                    
                    _loc_6 = _loc_4[_loc_7];
                    _loc_5 = _loc_5 + (" " + HTMLUtil.addColor(_loc_6.BossName, GlobalStyle.colorHong));
                    _loc_7++;
                }
                this.passTitleTxt.htmlText = _loc_5;
            }
            else
            {
                this.setNumberTxt(_loc_2.getPlayerMaxPass(copyCode));
                this.passTitleTxt.htmlText = "<font color=\'" + GlobalStyle.colorLv + "\'>" + Language.getString(71022) + "</font>";
            }
            this.enterCopyBtn.filterEnabled = !_loc_2.getIsMaxLv(copyCode, _loc_2.getPlayerMaxPass(copyCode));
            this.passInfo.refresh(param1);
            return;
        }// end function

        public function set towerScanStatus(param1:int) : void
        {
            var _loc_2:Boolean = true;
            this._sweepGuanDes.visible = true;
            this._sweepGuanTxt.visible = _loc_2;
            this._sweepGuanTxt.text = "" + Cache.instance.copy.tower.getHistoryMaxLv(copyCode);
            this._scanValue = param1;
            if (this._resLoad == false)
            {
                return;
            }
            if (param1 > 0)
            {
                this._bar.visible = true;
                this._secTimer.visible = true;
                this._sweepBtn.visible = true;
                this._getRewardBtn.visible = false;
                this._sweepBtn.enabled = false;
                this._resetBtn.enabled = false;
                this._resetBtn.filters = [FilterConst.colorFilter2];
                this._totleTime = (Cache.instance.copy.tower.getHistoryMaxLv(copyCode) - param1 + 1) * GameConst.TConstTowerScanSecondsPerLevel;
                this._secTimer.setLeftTime(this._totleTime);
                this._sweepGuanTxt.text = "" + (Cache.instance.copy.tower.getHistoryMaxLv(copyCode) - param1 + 1);
                var _loc_2:Boolean = true;
                this._sweepGuanDes.visible = true;
                this._sweepGuanTxt.visible = _loc_2;
            }
            else
            {
                this._bar.visible = false;
                this._secTimer.visible = false;
                this._getRewardBtn.visible = false;
                this._sweepBtn.enabled = true;
                this._resetBtn.enabled = true;
                this._resetBtn.filters = null;
                this._secTimer.stop();
                if (Cache.instance.pack.towerScanPackCache.hasItemByCopyCode(copyCode) || Cache.instance.copy.tower.getHasMoney(copyCode))
                {
                    this._getRewardBtn.visible = true;
                    this._sweepBtn.visible = false;
                }
                else
                {
                    this._getRewardBtn.visible = false;
                    this._sweepBtn.visible = true;
                }
                if (Cache.instance.copy.tower.getTowerInfo(copyCode).todayRestTimes >= GameConst.TConstTowerMaxResetOneDay)
                {
                    this._resetBtn.enabled = false;
                    this._resetBtn.filters = [FilterConst.colorFilter2];
                }
            }
            if (int(VipConfig.instance.getValueByTypeAndLv(EVipRecordType._EVipRecordTypeTowerScan, Cache.instance.vip.level)) <= 0)
            {
                var _loc_2:Boolean = false;
                this._sweepGuanDes.visible = false;
                this._sweepGuanTxt.visible = _loc_2;
            }
            if (Cache.instance.copy.tower.getPlayerMaxPass(copyCode) == Cache.instance.copy.tower.getHistoryMaxLv(copyCode))
            {
                var _loc_2:Boolean = false;
                this._sweepGuanDes.visible = false;
                this._sweepGuanTxt.visible = _loc_2;
            }
            return;
        }// end function

        public function scanEnd() : void
        {
            this._secTimer.stop();
            this._bar.visible = false;
            this._secTimer.visible = false;
            this._sweepBtn.visible = false;
            this._getRewardBtn.visible = true;
            this._resetBtn.enabled = true;
            this._resetBtn.filters = null;
            return;
        }// end function

        public function towerScanRewardSucceed() : void
        {
            this._sweepBtn.visible = true;
            this._getRewardBtn.visible = false;
            this._sweepBtn.enabled = true;
            return;
        }// end function

        private function onTowerInfoUpdate(param1:Object = null) : void
        {
            this.updateComBatTxt();
            this.refresh();
            return;
        }// end function

        private function updateComBatTxt() : void
        {
            var _loc_1:* = TowerCopyConfig.instance.getTowerTipInfo(TowerModule.copyCode, Cache.instance.copy.tower.getCurrentLevel(TowerModule.copyCode));
            if (this._combatTxt)
            {
                this._combatTxt.text = "" + _loc_1.power;
            }
            return;
        }// end function

        private function onSecViewTimeChange(event:DataEvent) : void
        {
            this._bar.setValue(int(event.data), this._totleTime);
            return;
        }// end function

        protected function onMouseClick(event:MouseEvent) : void
        {
            if (Cache.instance.copy.tower.getScanStatus(copyCode) > 0)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71023));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.TowerEnterCopy, copyCode));
            return;
        }// end function

        private function onReset(event:MouseEvent) : void
        {
            if (Cache.instance.copy.tower.getTowerInfo(copyCode).todayRestTimes >= GameConst.TConstTowerMaxResetOneDay)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71024));
                return;
            }
            if (Cache.instance.copy.tower.getPlayerMaxPass(copyCode) == 0)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71025));
                return;
            }
            if (GameMapUtil.curMapState.isTowerMap)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71026));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.TowerReset, copyCode));
            return;
        }// end function

        private function onSweep(event:MouseEvent) : void
        {
            if (int(VipConfig.instance.getValueByTypeAndLv(EVipRecordType._EVipRecordTypeTowerScan, Cache.instance.vip.level)) <= 0)
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71027));
                return;
            }
            if (Cache.instance.copy.tower.getPlayerMaxPass(copyCode) == Cache.instance.copy.tower.getHistoryMaxLv(copyCode))
            {
                MsgManager.showRollTipsMsg("" + Language.getString(71028));
                return;
            }
            Dispatcher.dispatchEvent(new DataEvent(EventName.TowerSweep, copyCode));
            return;
        }// end function

        private function onGetRewardBtn(event:MouseEvent) : void
        {
            if (TowerSweepBagPanel.isViewShow)
            {
                TowerSweepBagPanel.instance.hide();
            }
            else
            {
                TowerSweepBagPanel.instance.show();
            }
            return;
        }// end function

        private function setNumberTxt(param1:int) : void
        {
            if (param1 < 10)
            {
                this.numberTxt.text = param1.toString();
            }
            else
            {
                this.numberTxt.text = param1.toString();
            }
            return;
        }// end function

        protected function init3DRect() : void
        {
            this._rect3d = Rect3DManager.instance.registerWindow(true, this.rect, this);
            Rect3DManager.instance.windowShowHander(null, this);
            return;
        }// end function

        protected function remove3D() : void
        {
            if (this._rect3d)
            {
                Rect3DManager.instance.disposeRect3d(this._rect3d);
                this._rect3d = null;
            }
            return;
        }// end function

        public function get3DObject() : Rect3DObject
        {
            return this._rect3d;
        }// end function

    }
}
